package vn.isolar.object;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import org.apache.http.client.ClientProtocolException;

import vn.isolar.main.SvntestActivity;
import vn.isolar.system.gps.MyLocation;
import vn.isolar.system.gps.MyLocation.LocationResult;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.util.Log;
import android.widget.Toast;

public class Executable {
	private Intent intent;
	private int type = 0;	// Direction, sms, alarm
							// type = 1 mean get location
	public Executable(Intent i){
		this.intent = i;
	}
	public Executable(String cmd){
		if(cmd.equals("GET_LOCATION")){
			this.type = 1;				//Set type = get location
			
			
		}
	}
	public String getType(){
		if(type == 0)
			return "DIRECTION_SMS_ALARM";
		else
			return "GET_LOCATION"; 
	}
	public Intent createIntent(){
		return intent;
	}
	public Object execute(){
		return null;
		
	}

	public void execute(final Context context) {
		// TODO Auto-generated method stub		
		MyLocation myLocation = new MyLocation();
		LocationResult locationResult = new LocationResult(){
		    @Override
		    public void gotLocation(Location location){
		    	if(location != null){
			    	double lat = location.getLatitude();
			    	double lng = location.getLongitude();
			    	
			    	// Request TTS text to speech 
			        SvntestActivity.tomClient.execute("Ban dang o  " + getAddress(context, lat, lng));
		    	}
		    	else{
		    		SvntestActivity.tomClient.execute("xin loi, khong lay duoc vi tri");
		    	}
		    }
		};
		myLocation.getLocation(context, locationResult);
		
	}
	public String getAddress(Context context,double lat, double lng) {
	    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
	    String add = null;
	    try {
	        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
	        Address obj = addresses.get(0);
	        add = obj.getAddressLine(0);
	      
	        add = add + "\n" + obj.getCountryName();
	        add = add + "\n" + obj.getCountryCode();
	        add = add + "\n" + obj.getAdminArea();
	        add = add + "\n" + obj.getPostalCode();
	        add = add + "\n" + obj.getSubAdminArea();
	        add = add + "\n" + obj.getLocality();
	        add = add + "\n" + obj.getSubThoroughfare();

	        Log.v("IGA", "Address" + add);
	        // Toast.makeText(this, "Address=>" + add,
	        // Toast.LENGTH_SHORT).show();

	        // TennisAppActivity.showDialog(add);
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	        
	    }
	    return add;
	}

}
